const http = require('http')
const fs =require('fs')
const path = require('path')
const  server = http.createServer()
server.on('request',(req,res)=>{
    //1、 获取客户端的url地址
    const url = req.url
    // res.setHeader('Content-Type','text/html;charset=utf-8')
    //2、把请求的url地址映射为本地文件的存放路径
    //newhtml/index.html
    //newhtml/index.css
    //newhtml/index.js
    //   const fpath= path.join(__dirname,url)
    let fpath = ''
    if(url==='/'){
        fpath=path.join(__dirname,'./newhtml/index.html')
    }else{
        fpath = path.join(__dirname,'./newhtml',url)
    }
      console.log(fpath);
    // 3、根据映射过来的文件路径读取文件的内容
    fs.readFile(fpath,'utf8',(err,dataStr)=>{
        if(err){
            return res.end('404 not found')
        }else{
            res.end(dataStr)
        }
    })   
})
server.listen(80,()=>{
    console.log('server runing at http://127.0.0.1');
})